#include "pg.h"
#include "connexionbdd.h"
//#include <iostream>

void Pg::init(QSqlDatabase sqlDb)
{
    db = sqlDb;

    if(!sqlDb.isOpen()){
        db = QSqlDatabase::database("guest");
    }

    fams = 0;
    proms = "An 404";
    id = 0;
    solde = 0.0;
    actif = 0;
}

Pg::Pg(QSqlDatabase sqlDb)
{
    init(sqlDb);
}

Pg::Pg( int i, QSqlDatabase sqlDb)
{
    init(sqlDb);

    id = i;

    completer();
}

Pg::Pg( int fam, QString prom, QSqlDatabase sqlDb)
{
    init(sqlDb);

    fams = fam;
    setProms(prom);

    completer();
}


Pg::~Pg(){
}

void Pg::setProms(QString p){
    proms = p;
}

int Pg::getFams(){
    return fams;
}

int Pg::getId(){
    return id;
}

double Pg::getSolde(){
    return solde;
}

QString Pg::getProms()
{
    return proms;
}

QString Pg::getBucque(){
    return bucque;
}

bool Pg::completer(){

        //Si la fam's et la prom's sont de la bonne forme, on recherche le Pg
        db.transaction();
        QSqlQuery q(db);    // On créer la requète de recherche des informations manquantes.
        if(!(proms=="An 404")){
            q.prepare("SELECT id, bucque, fams, solde, proms FROM v_users WHERE fams=:fams AND proms=:proms AND actif=1");
            q.bindValue(":fams",fams);
            q.bindValue(":proms",proms);
        }
        else if(id > 0)
        {
            q.prepare("SELECT id, bucque, fams, solde, proms FROM v_users WHERE id = :id");
            q.bindValue(":id",id);
        }
        else
        {
            return false;
        }
        q.exec();

        if(q.first())
        {
            // Si on a un premier resultat, on complète les variables
            id = q.value(0).toInt();
            bucque = q.value(1).toString();
            fams = q.value(2).toInt();
            solde = q.value(3).toDouble();
            proms = q.value(4).toString();
        }
        else
        {
            id = -1;
            return false;
        }
        q.finish();
        db.commit();

    return true;
}

QStringList Pg::listProms(int fams, QSqlDatabase db){
    QStringList l;
    db.transaction();
    QSqlQuery q(db);
    //On prépare une requete pour récuperer les prom's dans lesquelles la fam's demandée est associée à un compte actif
    q.prepare("SELECT proms FROM v_users WHERE fams = :fams GROUP BY proms");
    q.bindValue(":fams",fams);
    q.exec();
    //On insère les prom's dans la liste
    while(q.next()){
        l.append(q.value(0).toString());
    }
    //On termine la requète
    q.finish();
    db.commit();

    return l;
}

QList<histo_struct> Pg::getHisto()
{
    //std::cout << "getHisto()\n";

    QList<histo_struct> l;

    db.transaction();
    QSqlQuery q(db);

    q.prepare("SELECT intitule, montant, DATE_FORMAT(hdate,'%Y-%m-%d à %H:%i') AS ladate, COUNT(*) FROM v_hist WHERE compte=:id GROUP BY ladate, intitule ORDER BY ladate DESC LIMIT 0,30");
    q.bindValue(":id", id);
    q.exec();

    while(q.next())
    {
        struct histo_struct histo_ligne;

        histo_ligne.intitule = q.value(0).toString();
        histo_ligne.montant = q.value(1).toDouble() * q.value(3).toDouble();
        histo_ligne.nb = q.value(3).toDouble();
        histo_ligne.date = q.value(2).toString();

        l.append(histo_ligne);
    }

    q.finish();
    db.commit();

    //std::cout << "FIN getHisto()\n";

    return l;
}

QList<QStringList> Pg::getTrans()
{
    QList<QStringList> l;

    db.transaction();
    QSqlQuery q(db);

    q.prepare("SELECT libelle, montant, deb_pseudo, deb_proms, cre_pseudo, cre_proms, DATE_FORMAT(tdate,'%Y-%m-%d à %H:%i') AS ladate FROM v_transactions WHERE (cre_fams = :f  AND cre_proms = :p) OR (deb_fams = :f  AND deb_proms = :p) ORDER BY ladate DESC");
    q.bindValue(":f", fams);
    q.bindValue(":p",proms);
    q.exec();

    while(q.next())
    {
        QStringList trans_ligne;

        trans_ligne.append(q.value(0).toString());
        trans_ligne.append(q.value(1).toString());
        trans_ligne.append(q.value(2).toString());
        trans_ligne.append(q.value(3).toString());
        trans_ligne.append(q.value(4).toString());
        trans_ligne.append(q.value(5).toString());
        trans_ligne.append(q.value(6).toString());

        l.append(trans_ligne);
    }

    q.finish();
    db.commit();

    //std::cout << "FIN getHisto()\n";

    return l;
}

int Pg::getPin()
{
    db.transaction();
    QSqlQuery q(db);

    q.prepare("SELECT code FROM v_users WHERE id=:id");
    q.bindValue(":id", id);
    q.exec();

    if(q.first())
    {
        qDebug() << q.value(0).toInt();
        return q.value(0).toInt();
    }

    q.finish();
    db.commit();

    return -1;
}
